package ctrLayer;
import javax.swing.JComboBox;

import modelLayer.*;

/**
 * ForestOwnerCtr is responsible for the interaction with the modelLayer
 * @author Thomas Bonderup
 * @version 1.0.0
 */
public class ForestOwnerCtr {

	//instance variables
	private OwnerContainer oCon;
	private ForestContainer fCon;
	private TreeCtr tCtr;
	private static ForestOwnerCtr instance;
	
	/**
	 * Constructor the class ForestOwnerCtr
	 */
	public ForestOwnerCtr() {
		oCon = OwnerContainer.getInstance();
		fCon = ForestContainer.getInstance();
		tCtr = TreeCtr.getInstance();
	}
	
	/**
	 * Singleton
	 * @return instance
	 */
	public static ForestOwnerCtr getInstance() {
		if(instance == null) {
			instance = new ForestOwnerCtr();
		}
		return instance;
	}
	
	/**
	 * Creates an Owner and adds it to the container
	 * @param ownerName
	 * @param address
	 * @param phoneNumber
	 * @return 
	 */
	public boolean createOwner(String ownerName, String address, String phoneNumber) {
		boolean resultat = false;
		try {
			Owner owner = new Owner(ownerName, address, phoneNumber);
			oCon.addOwner(owner);
		}catch (Exception e) {
			resultat = false;
		}
			return resultat;
	}
	
	/**
	 * Creates an Forest and adds it to the container
	 * @param name
	 * @param location
	 * @param squareKiloMetre
	 * @param natureOfTrees
	 * @param price
	 * @return 
	 */
	public boolean CreateForest(String name, String location, double squareKiloMetre, String natureOfTrees, double price, int ownerNr, int treeId) {
		boolean resultat = false;
		try {
			
			Owner forestOwner = findOwner(ownerNr);
			Tree treeType = tCtr.findTree(treeId);
			Forest forest = new Forest(name, location, squareKiloMetre, natureOfTrees, price, forestOwner, treeType);
			fCon.addForest(forest);
			resultat = true;
		}catch (Exception e) {
			resultat = false;
		}
		return resultat;
	}
	
	/**
	 * Find an Owner on owner name
	 * @param ownerName
	 * @return Owner object
	 */
	public Owner findOwner(int id) {
		
		int index = 0;
		boolean found = false;
		while(index < oCon.owners().size() && found != true) {
			Owner o = oCon.getOwner(index);
			if(o.getOwnerId() == id) {
				found = true;
			}
			else{
				index++;
			}
		}
		if(found == true) {
			return oCon.getOwner(index);
		}
		else {
			return null;
		}
	}
	
	/**
	 * Find a forest on forest name
	 * @param name
	 * @return Forest object
	 */
	public Forest findForets(String name) {
		
		int index = 0;
		boolean found = false;
		while(index < fCon.forests().size() && found != true) {
			Forest f = fCon.getForest(index);
			if(f.getName() == name) {
				found = true;
			}
			else {
				index++;
			}
		}
		if(found == true) {
			return fCon.getForest(index);
		}
		return null;
	}
	
	/**
	 * Deletes the forest 
	 * @param id
	 */
	public void deleteForest(int id) {
		int index = 0;
		
		while(index < fCon.forests().size()) {
			Forest forest = fCon.getForest(index);
			if(forest.getForestNr() == id) {
				fCon.deleteForest(index);
			}
			index++;
		}
	}
	
	
	/**
	 * Method that adds the forests to a 2 dimensional array, so it can be used in Jtable
	 * @return
	 */
	public String[][] forestListToArray() {
		int index = 0;
		String data[][] = new String[fCon.forests().size()][8];
		while(index < fCon.forests().size()) {
			Forest f = fCon.getForest(index);
			
			data[index][0] = Integer.toString(f.getForestNr());
			data[index][1] = f.getName();
			data[index][2] = f.getLocation();
			data[index][3] = Double.toString(f.getSquareKiloMetre());
			data[index][4] = f.getNatureOfTrees();
			data[index][5] = Double.toString(f.getPrice());
			data[index][6] = f.getForestOwner().getOwnerName();
			data[index][7] = f.getTreeType().getSort();
			
			index++;
		}
			
		return data;
	}
	
	/**
	 * Method that adds the owners to a 2 dimensional array, so it can be used in Jtable
	 * @return
	 */
	public String[][] ownerListToArray() {
		int index = 0;
		String data[][] = new String[oCon.owners().size()][4];
		while(index < oCon.owners().size()) {
			Owner o = oCon.getOwner(index);
			
			data[index][0] = Integer.toString(o.getOwnerId());
			data[index][1] = o.getOwnerName();
			data[index][2] = o.getAddress();
			data[index][3] = o.getPhoneNumber();
			
			index++;
		}
			
		return data;
	}
	
	/**
	 * Sets up the array to combobox
	 * @return
	 */
	public String[] listToArrayCombobox() {
		int index = 0;
		String data[] = new String[oCon.owners().size()];
		while(index < oCon.owners().size()){
			Owner o = oCon.getOwner(index);
			
			data[index] = o.getOwnerName();
			
			index++;
		}
		return data;
	}
	
}
